package android.support.v4.os;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.edroid.autorunner.um.UserInfo;
import com.edroid.autorunner.um.UserManager;

/**
 * 
 * @author 建宾 2014-9-25
 *
 */
public class EResources extends EHook {

	@Override
	public String getClassName() {
		return Resources.class.getName();
	}

	@Override
	public void hook() {
		findAndHookMethod("getDisplayMetrics", this);
		
		//public void updateConfiguration(Configuration config, ...)
		hookAllMethods("updateConfiguration");
	}

	@Override
	protected void after(String name, Object[] args, MethodHookParam param) {
		if("getDisplayMetrics".equals(name)) {
			DisplayMetrics dm = (DisplayMetrics) param.getResult();

			if(sHookResolution) {
				UserInfo userInfo = UserManager.currentUserInfo();
				dm.widthPixels = userInfo.screenWidth;
				dm.heightPixels = userInfo.screenHeight;
				dm.densityDpi = userInfo.densityDpi;
			}
		} else if ("updateConfiguration".equals(name)) {
			if (args.length > 0 && args[0] != null && args[0] instanceof Configuration) {
				String imsi = UserManager.currentUserInfo().imsi;
				if(!TextUtils.isEmpty(imsi)) {
					Configuration config = new Configuration((Configuration) args[0]);
					config.mcc = Integer.parseInt(imsi.substring(0, 3));
					config.mnc = imsi.charAt(4) - '0';
					
					param.args[0] = config;
				}
			}
		}
	}
}
